﻿using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using My.Admin.Dto.System;
using My.Admin.Enum;
using My.Admin.Model;
using My.Admin.Model.System;
using My.Admin.Repository;
using My.Admin.Service.System.IService;
using My.Core.CustomException;
using My.Core.Enums;
using My.Core.SqlSugar.model;
using System.ComponentModel;

namespace My.Admin.Service.System
{
	public class SysDictTypeService : BaseService<SysDictType>, ISysDictTypeService
	{
		private readonly ISysDictDataService _sysDictDataService;

		public SysDictTypeService(ISysDictDataService sysDictDataService)
		{
			_sysDictDataService = sysDictDataService;
		}

		/// <summary>
		/// 获取字典类型分页列表
		/// </summary>
		/// <returns></returns>
		public async Task<PagedInfo<SysDictType>> Page(PageDictTypeInput input)
		{
			var code = !string.IsNullOrEmpty(input.Code?.Trim());
			var name = !string.IsNullOrEmpty(input.Name?.Trim());
			var lSysDictType = AsQueryable()
			   .WhereIF(code, u => u.Code.Contains(input.Code))
			   .WhereIF(name, u => u.Name.Contains(input.Name))
			   .OrderBy(u => new { u.OrderNo, u.Code });
				
			return await lSysDictType.ToPageAsync(input);
		}

		/// <summary>
		/// 获取字典类型列表
		/// </summary>
		/// <returns></returns>
		public async Task<List<SysDictType>> GetList()
		{
			return await AsQueryable().OrderBy(u => new { u.OrderNo, u.Code }).ToListAsync();
		}

		/// <summary>
		/// 获取字典类型-值列表
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<List<SysDictData>> GetDataList([FromQuery] GetDataDictTypeInput input)
		{
			var dictType = await GetFirstAsync(u => u.Code == input.Code);
			if (dictType == null)
				throw new CustomException(ResultCode.CUSTOM_ERROR, "字典类型不存在", "字典类型不存在");
			return await _sysDictDataService.GetDictDataListByDictTypeId(dictType.Id);
		}

		/// <summary>
		/// 添加字典类型
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task AddDictType(AddDictTypeInput input)
		{
			var isExist = await IsAnyAsync(u => u.Code == input.Code);
			if (isExist)
				throw new CustomException(ResultCode.CUSTOM_ERROR, "字典类型已存在,名称或编码重复", "字典类型已存在,名称或编码重复");
			await InsertAsync(input.Adapt<SysDictType>());
		}

		/// <summary>
		/// 更新字典类型
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task UpdateDictType(UpdateDictTypeInput input)
		{
			var isExist = await IsAnyAsync(u => u.Id == input.Id);
			if (!isExist)
				throw new CustomException(ResultCode.CUSTOM_ERROR, "字典类型不存在", "字典类型不存在");
			isExist = await IsAnyAsync(u => u.Code == input.Code && u.Id != input.Id);
			if (isExist)
				throw new CustomException(ResultCode.CUSTOM_ERROR, "字典类型已存在,名称或编码重复", "字典类型已存在,名称或编码重复");

			await UpdateAsync(input.Adapt<SysDictType>());
		}

		/// <summary>
		/// 删除字典类型
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task DeleteDictType(DeleteDictTypeInput input)
		{
			var dictType = await GetFirstAsync(u => u.Id == input.Id);
			if (dictType == null)
				throw new CustomException(ResultCode.CUSTOM_ERROR, "字典类型不存在", "字典类型不存在");

			//删除字典值
			await DeleteAsync(dictType);
			await _sysDictDataService.DeleteDictData(input.Id);
		}

		/// <summary>
		/// 获取字典类型详情
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<SysDictType> GetDetail([FromQuery] DictTypeInput input)
		{
			return await GetFirstAsync(u => u.Id == input.Id);
		}

		/// <summary>
		/// 修改字典类型状态
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task SetStatus(DictTypeInput input)
		{
			var dictType = await GetFirstAsync(u => u.Id == input.Id);
			if (dictType == null)
				throw new CustomException(ResultCode.CUSTOM_ERROR, "字典类型不存在", "字典类型不存在");

			//if (!Enum.IsDefined(typeof(StatusEnum), input.Status))
			//	throw new CustomException(ResultCode.CUSTOM_ERROR, "字典状态错误", "字典状态错误");
			
			dictType.Status = (StatusEnum)input.Status;
			await UpdateAsync(dictType);
		}

		/// <summary>
		/// 获取所有字典集合
		/// </summary>
		/// <returns></returns>
		public async Task<List<SysDictType>> GetAllDictList()
		{
			return await AsQueryable().Includes(u => u.Children).ToListAsync();
		}
	}
}
